import { app, BrowserWindow, ipcMain,Tray,Menu,shell } from 'electron'
import path from 'node:path'
// import {createTray} from './tray.js'
// import contextTest from './contextMenu.js'

// The built directory structure
//
// ├─┬─┬ dist
// │ │ └── index.html
// │ │
// │ ├─┬ dist-electron
// │ │ ├── main.js
// │ │ └── preload.js
// │
process.env.DIST = path.join(__dirname, '../dist')
process.env.VITE_PUBLIC = app.isPackaged ? process.env.DIST : path.join(process.env.DIST, '../public')
let win: BrowserWindow | null


// 🚧 Use ['ENV_NAME'] avoid vite:define plugin - Vite@2.x
const VITE_DEV_SERVER_URL = process.env['VITE_DEV_SERVER_URL']

function createWindow() {
  win = new BrowserWindow({
    icon: path.join(process.env.VITE_PUBLIC, 'logo.png'),
    // icon: path.join(__dirname, 'electron-vite.svg'),
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
      devTools:true,
      nodeIntegration:true,
      contextIsolation: false,
      webSecurity: false
    },
    width:400,
    height:600,
    // width:1920,
    // height:1080,
    resizable:false,
    center:true,
    frame:false,
    show:false
  })

  ipcMain.on('window-max',function(){
    win?.setSize(1020,720)
    // win?.setSize(1920,1080)
    win?.center()
  })
  ipcMain.on('window-min',function(){
    win?.setContentSize(400,600)
    // win?.setContentSize(1920,1080)
    win?.center()
  })
  ipcMain.on('set-min-window',function(){
    win?.minimize()
  })
  ipcMain.on('set-close-window',function(){
    win?.hide()
  })
  ipcMain.on('open-sign-url',function(event,url){
    shell.openExternal(url)
  })
  // Test active push message to Renderer-process.
  win.webContents.on('did-finish-load', () => {
    win?.webContents.send('main-process-message', (new Date).toLocaleString())
  })
  win.on('ready-to-show',function(){
    win?.show()
  })

  if (VITE_DEV_SERVER_URL) {
    win.loadURL(VITE_DEV_SERVER_URL)
  } else {
    // win.loadFile('dist/index.html')
    win.loadFile(path.join(process.env.DIST, 'index.html'))
  }
 
}

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.

app.on('window-all-closed', () => {
  
  
  if (process.platform !== 'darwin') {
    app.quit()
    win = null
  }
})

app.on('activate', () => {
  // On OS X it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow()
  }
})


app.on('ready',async()=>{
const tray = new Tray(path.join(process.env.VITE_PUBLIC, 'logo (1).png'))
const trayContextMenu = Menu.buildFromTemplate([
  {
    label: 'show',
    
    click: () => {
      win?.show()
    }
  },
  {
    label: 'quit',
    role: 'quit'
  }
])
tray.setToolTip('WingsVPN')
tray.on('click',()=>{
  win?.show()
})
tray.on('right-click', () => {
  tray.popUpContextMenu(trayContextMenu)
})
// createWindow()
})

app.whenReady().then(createWindow)
